package cn.edu.zucc.usedcarsales.control;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.zucc.usedcarsales.itf.ITransferOwnershipInformationManager;
import cn.edu.zucc.usedcarsales.model.TransferOwnershipInformation;
import cn.edu.zucc.usedcarsales.util.DBUtil;

public class TransferOwnershipInformationManager implements ITransferOwnershipInformationManager {

	@Override
	public void save(TransferOwnershipInformation tinfo) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement(
					"insert into TransferOwnershipInformation "
					+ "(VehicleID,DoneDealTime,DoneDealPrice,BuyerID,SellerID) "
					+ "values(?,?,?,?,?)");
			pst.setInt(1, tinfo.getVehicleID());
			pst.setString(2, tinfo.getDoneDealTime());
			pst.setString(3, tinfo.getDoneDealPrice());
			pst.setInt(4, tinfo.getBuyerID());
			pst.setInt(5, tinfo.getSellerID());
			pst.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public List<TransferOwnershipInformation> getAll() {
		// TODO Auto-generated method stub
		List<TransferOwnershipInformation> list = new ArrayList<TransferOwnershipInformation>();
		try{
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from TransferOwnershipInformation");
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				TransferOwnershipInformation t= new TransferOwnershipInformation();
				t.setBuyerID(rs.getInt(5));
				t.setDoneDealPrice(rs.getString(4));
				t.setDoneDealTime(rs.getString(3));
				t.setSellerID(rs.getInt(6));
				t.setTransferOwnershipID(rs.getInt(1));
				t.setVehicleID(rs.getInt(2));
				list.add(t);
			}
		}catch(SQLException e){
			
		}
		return list;
	}

}
